package com.xingkeduo.controller.hairdresser;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.xingkeduo.configuration.RedisListOpsService;
import com.xingkeduo.controller.base.InitController;
import com.xingkeduo.model.param.MarketingParam.MarketingTypeEnum;
import com.xingkeduo.service.hairdresser.HairdresserService;
import com.xingkeduo.util.Constant;
import com.xingkeduo.util.RedisKeyEnum;
import com.xingkeduo.util.tool.ResponseMessage;

/**
 * 
* @ClassName: HairdresserProvideController
* @Description: 发型师端券，卡营销
* @author lixin
* @date Aug 19, 2019 3:21:21 PM
*
 */
@RestController
@RequestMapping(Constant.API_URL_PREFIX + "/hairdresser/provide")
public class HairdresserProvideController extends InitController {
	Logger logger =LoggerFactory.getLogger(HairdresserProvideController.class);

	@Autowired
	HairdresserService hairdresserService;
	@Autowired
	RedisListOpsService redisListOpsService;
	/**
	 * 
	* @Title: getMarketingSellList 
	* @Description: 发型师端查询售卖券包列表
	* @param @param storeId
	* @param @return    设定文件 
	* @return String    返回类型 
	* @throws 
	* @author lixin
	* @date Aug 19, 2019 3:25:58 PM
	 */
	@RequestMapping("/getMarketingSellList")
	public ResponseMessage getMarketingSellList(Long storeId) {
		if(storeId==null) {
			return ResponseMessage.error("参数缺失", 500);
		}
		return hairdresserService.getMarketingSellListByStoreId(storeId);
	}
	/**
	 * 
	* @Title: getMarketingSellDetail 
	* @Description: 查询售卖券包详情 
	* @param @param marketingId
	* @param @return    设定文件 
	* @return String    返回类型 
	* @throws 
	* @author lixin
	* @date Aug 19, 2019 3:29:17 PM
	 */
	@RequestMapping("/getMarketingSellDetail")
	public ResponseMessage getMarketingSellDetail(Long marketingId,Integer marketingType) {
		if(marketingId==null) {
			return ResponseMessage.error("参数缺失", 500);
		}
		if(marketingType==null) {
			return ResponseMessage.error("参数缺失", 500);
		}
		if(MarketingTypeEnum.parseStatus(marketingType)==MarketingTypeEnum.UNKNOWN) {
			return ResponseMessage.error("参数类型未知", 500);
		}
		return hairdresserService.getMarketingDetail(marketingId,marketingType);
	}
	/**
	 * 
	* @Title: bindBagReceiveAccount 
	* @Description: 售卖领取优惠包 
	* @param @param code
	* @param @param userId
	* @param @return    设定文件 
	* @return ResponseMessage    返回类型 
	* @throws 
	* @author lixin
	* @date Aug 21, 2019 2:43:14 PM
	 */
	@RequestMapping("/bindBagReceiveAccount")
	public ResponseMessage bindBagReceiveAccount(Long marketingId,Integer marketingType,Long userId) {
		if(marketingId==null) {
			return ResponseMessage.error("参数缺失", 500);
		}
		if(marketingType==null) {
			return ResponseMessage.error("参数缺失", 500);
		}
		if(userId==null) {
			return ResponseMessage.error("参数缺失", 500);
		}
		boolean isExcep = true;
	    long EXPIRE_TIME = 30 * 1000;
	    String keyName = RedisKeyEnum.RECEIVE_COUPONBAG_SELL_KEY.getKeyName(marketingId+":"+userId);
		long _time1 = System.currentTimeMillis();
		try {
			// 创建同步锁,防止任务阻塞重复执行
			boolean locked = redisListOpsService.locked(keyName, "LOCK",EXPIRE_TIME);
			if (!locked) {
				isExcep = false;
				logger.info("【优惠包售卖】售卖中。。。。");
				return ResponseMessage.error("售卖中", 500);
			}
			return hairdresserService.bindBagReceiveAccount(marketingId,marketingType,userId);
		}catch(Exception e) {
			logger.error("优惠包售卖失败,e:{}",e);
			return ResponseMessage.error("操作失败", 500);
		}finally {
			if(isExcep){
				redisListOpsService.unlock(keyName);
				long _time2 = System.currentTimeMillis();
				logger.info("【优惠包售卖】解锁key： "+ keyName + "+ 耗时：" + (_time2 - _time1));
			}
		}
		
	}
}
